var $ = require('../../utils/Ajax.js');
Page({
  data: {
    Id: null,
    // 图片地址
    PictureUrl: getApp().PictureUrl,
    Phone: '',
    PhoneIcon: "../../images/icon_rxg.png",
    PhoneFocus: false,
    PhoneDisabled: true,
    Img: null,
    Title: null,
    Add: null,
    Time: null,
    // 可以的总票数
    Num: null,
    // 默认人数
    Number: 1,
    // 单价
    Price: 0,
    // 总价
    Price_: 0,
    ExperPrice_: 0,
    Radio: true,
    Jia: true,
    Jian: false,
    Disabled: false
  },
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: "确认订单"
    }),
    wx.setNavigationBarColor({
      frontColor: '#000000',
      backgroundColor: '#FDD436'
    })
    if (Number(options.Num) > 0) {
      this.setData({
        Number: 1,
        Price_: Number(options.Price),
        Jia: true,
        Jian: false
      })
    } else {
      this.setData({
        Number: 0,
        Jia: true,
        Jian: true
      })
    }
    this.setData({
      Id: options.Id,
      Title: options.Title,
      Add: options.Add,
      Img: options.Img,
      Time: options.Time,
      Num: Number(options.Num),
      Price: Number(options.Price)
    })
  },
  onShow: function () {
    var Token = wx.getStorageSync('Token');
    if (Token == '' || Token == null) {
      wx.switchTab({
        url: '../My/My',
      })
    }
  },
  // 点击减少体验人数
  Increase: function (){
    var Number = this.data.Number;
    --Number;
    if (Number == 1) {
      this.setData({
        Number: Number,
        Price_: this.data.Price * Number,
        Jia: true,
        Jian: false
      })
    }else{
      this.setData({
        Number: Number,
        Price_: this.data.Price * Number,
        Jia: false,
        Jian: false
      })
    }
  },

  // 点击增加体验人数
  Reduce: function () {
    var Number = this.data.Number
    ++Number
    if (this.data.Number+1 == this.data.Num) {
      this.setData({
        Number: Number,
        Price_: this.data.Price * Number,
        Jia: false,
        Jian: true
      })
    } else {
      this.setData({
        Number: Number,
        Price_: this.data.Price * Number,
        Jia: false,
        Jian: false
      })
    }
  },

  // 监听
  CompletePhone: function (e) {
    var Phone = e.detail.value
    if (Phone.length == 11) {
      this.setData({
        Phone: Phone,
        PhoneFocus: false,
        PhoneDisabled: true
      });
      wx.showToast({
        title: '输入完成',
        icon: 'success',
        duration: 3000
      });
    }
  },

  // 点击编辑手机号码
  ClickPhoneIcon: function (){
    this.setData({
      PhoneIcon: "../../images/icon_rxg.png",
      PhoneFocus: true,
      PhoneDisabled: false
    });
  },

  // 阅读退款协议
  Radio: function () {
    this.setData({
      Radio: !this.data.Radio
    })
  },

  SubmitOrder: function () {
    var that = this;
    that.setData({
      Disabled: true
    })
    if (this.data.Radio) {
      var Id = that.data.Id;
      var Number = that.data.Number;
      var Phone = that.data.Phone;
      var Price = that.data.Price;
      var Time = that.data.Time;
      var Time_ = Time.split(" ");
      if (Phone.length != 11) {
        that.setData({
          Disabled: false
        })
        wx.showToast({
          title: '手机号不能为空',
          icon: 'none',
          image: '../../images/err.png',
          duration: 3000
        });
      } else if (Number == 0) {
        that.setData({
          Disabled: false
        })
        wx.showToast({
          title: '人数不能为0',
          icon: 'none',
          image: '../../images/err.png',
          duration: 3000
        });
      }else{
        console.log(that.data.Price_)
        $.Ajax("api/x_make_order", "POST", {
          obj_id: Id,
          order_type: 2,
          mobile: Phone,
          order_amount: that.data.Price_,
          obj_num: Number,
          obj_price: Price,
          date: Time_[0],
          time: Time_[1]
        }, function (res) {
          that.setData({
            Disabled: false
          })
          if (res.s) {
            wx.requestPayment({
              timeStamp: res.d.config.timestamp,
              nonceStr: res.d.config.nonceStr,
              package: res.d.config.package,
              signType: res.d.config.signType,
              paySign: res.d.config.paySign,
              success(res) {
                wx.navigateTo({
                  url: '../PaymentOk/PaymentOk?Many=' + that.data.Price_,
                })
              },
              fail: function (ress) {
                $.Ajax("api/x_d_order", "POST", {
                  order_sn: res.d.order_sn
                }, function (res) {
                  console.log(res)
                })
              }
            })
          } else {
            that.setData({
              Disabled: false
            })
            wx.showToast({
              title: res.m,
              icon: 'none',
              duration: 3000
            });
          }
        });
      }
    } else {
      that.setData({
        Disabled: false
      })
      wx.showToast({
        title: '请先阅读协议',
        icon: 'success',
        image: "../../images/err.png",
        duration: 3000
      });
    }
  }
})